﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QLBV
{
    public partial class Login : System.Web.UI.Page
    {
        String strUser="";
        String strPass="";
        String strMsg;
        String strConn = "Data Source=HTD-COM;Initial Catalog=QLBV;Integrated Security=True;Pooling=False";
            bool bolIsAdmin = false;
    bool bolIsLoged = false;       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["MaUser"] != null)
                {
                    TextBox1.Text = Request.Cookies["MaUser"].Value;
                    TextBox2.Text = Request.Cookies["MaPass"].Value;
                    check.Checked = true;
                }
            }
        }
         public String UserName
    {
        set { strUser = value; }
        get { return strUser; }
    }

    public String Password
    {
        set { strPass = value; }
        get { return strPass; }
    }

    public String Message
    {
        set { strMsg = value; }
        get { return strMsg; }
    }

    public bool IsAdmin
    {
        //set { bolIsAdmin = value; }
        get { return bolIsAdmin; }
    }

    public bool IsLoged
    {
        //set { bolIsLoged=value;}
        get { return bolIsLoged; }
    }
        public void clsLogIn(string username, string password)
    {
        strUser = username;
        strPass = password;
        LogIn();
    }
         public void LogIn()
         { 
         SqlConnection myCon = new SqlConnection(strConn);
        // try
         //{
             myCon.Open();
             String strChuoi = @"SELECT * FROM Users where Username=@username";
             SqlCommand myCommand = new SqlCommand(strChuoi, myCon);
               myCommand.Parameters.Add("@username", UserName);
             SqlDataReader reader = myCommand.ExecuteReader();
            
             if (reader.Read())
             {
                  
                   if (reader.GetValue(1).ToString() != strPass)
                     {
                         bolIsLoged = false;
                         strMsg = "Mật khẩu không đúng!!!!";
                     }
                     else
                     {
                         bolIsLoged = true;
                         Session["Dang nhap"] ="1";
                         Session["User"] = UserName;
                         
                     }
                 
             }
             else
             {
                 bolIsLoged = false;
                 strMsg = "Tên đăng nhập không đúng!!!!";
             }
             reader.Close();
             myCon.Close();
        // }
        // catch (Exception e)
         { 
         
         }
    }
         
        protected void Button1_Click(object sender, EventArgs e)
        {
            clsLogIn(TextBox1.Text.Trim(),TextBox2.Text.Trim());
            if (IsLoged==true)//neu dung user va pass
            {
                //tao cookie 
                HttpCookie ckUser = new HttpCookie("MaUser", TextBox1.Text);
                HttpCookie ckPass = new HttpCookie("MaPass", TextBox2.Text);
                if (check.Checked)
                {

                    //set thoi gian song cho cookie
                    ckUser.Expires = DateTime.Now.AddMonths(1);
                    ckPass.Expires = DateTime.Now.AddMonths(1);
                    Label3.Text = "Tai khoan da duoc ghi nho!";
                }
                else
                {
                    ckUser.Expires = DateTime.Now.AddMonths(-1);
                    ckPass.Expires = DateTime.Now.AddMonths(-1);
                }
                //tra cookie ve phia nguoi dang nhap
                Response.Cookies.Add(ckUser);
                Response.Cookies.Add(ckPass);
                Response.Redirect("index.aspx");// hien thi trang index.aspx
            }
            else
            {
                Label3.Text = Message; //Thông báo lỗi ra màn hình
            }
        }
    }
}